import Vue from 'vue'
import Vuex from 'vuex'

import persist from 'vuex-persist'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    shop:[]
  },
  mutations: {
    add(state,obj){
      let flag=true
      state.shop.forEach(item=>{
        if(item._id==obj._id){
          item.num++
          flag=false
        }
      })
      if(flag==true){
        obj.num=1
        state.shop.push(obj)
      }
    },
    changeNum(state,obj){ //改变数量
      state.shop.forEach(item=>{
        if(item._id==obj._id){
          item.num=obj.num
        }
      })
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
        storage: window.localStorage,
    }).plugin,
],
})
